package com.nbcio.pm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.pm.domain.PmTeamPermission;
import com.nbcio.pm.domain.bo.PmTeamPermissionBo;
import com.nbcio.pm.domain.vo.PmTeamPermissionVo;
import com.nbcio.pm.service.IPmTeamPermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团队权限
 *
 * @author nbacheng
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/team/permission")
public class PmTeamPermissionController extends BaseController {

    private final IPmTeamPermissionService pmTeamPermissionService;

    /**
     * 查询团队权限列表
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/list")
    public TableDataInfo<PmTeamPermissionVo> list(PmTeamPermissionBo bo, PageQuery pageQuery) {
        return pmTeamPermissionService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询团队权限详细
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/{permissionId}")
    public R<PmTeamPermissionVo> getInfo(@PathVariable Long permissionId) {
        return R.ok(pmTeamPermissionService.queryById(permissionId));
    }

    /**
     * 新增团队权限
     */
    @SaCheckPermission("pm:team:manage")
    @Log(title = "团队权限", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTeamPermissionBo bo) {
        return toAjax(pmTeamPermissionService.insertByBo(bo));
    }

    /**
     * 修改团队权限
     */
    @SaCheckPermission("pm:team:manage")
    @Log(title = "团队权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTeamPermissionBo bo) {
        return toAjax(pmTeamPermissionService.updateByBo(bo));
    }

    /**
     * 删除团队权限
     */
    @SaCheckPermission("pm:team:manage")
    @Log(title = "团队权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{permissionIds}")
    public R<Void> remove(@PathVariable Long[] permissionIds) {
        return toAjax(pmTeamPermissionService.deleteWithValidByIds(List.of(permissionIds), true));
    }

    /**
     * 根据权限编码查询权限
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/code/{permissionCode}")
    public R<PmTeamPermissionVo> getInfoByCode(@PathVariable String permissionCode) {
        return R.ok(pmTeamPermissionService.queryByPermissionCode(permissionCode));
    }

    /**
     * 查询角色拥有的权限列表
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/role/{roleId}")
    public R<List<PmTeamPermissionVo>> getRolePermissions(@PathVariable Long roleId) {
        return R.ok(pmTeamPermissionService.queryPermissionsByRoleId(roleId));
    }

    /**
     * 查询用户拥有的权限列表
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/user/{userId}")
    public R<List<PmTeamPermissionVo>> getUserPermissions(@PathVariable Long userId) {
        return R.ok(pmTeamPermissionService.queryPermissionsByUserId(userId));
    }

    /**
     * 查询权限树结构
     */
    @SaCheckPermission("pm:team:view")
    @GetMapping("/tree")
    public R<List<PmTeamPermissionVo>> getPermissionTree() {
        List<PmTeamPermissionVo> permissions = pmTeamPermissionService.queryPermissionTree();
        List<PmTeamPermissionVo> tree = pmTeamPermissionService.buildPermissionTree(permissions);
        return R.ok(tree);
    }
}
